<?php
namespace app\admin\controller;

use think\Db;
use think\Session;
use think\Validate;

class Double extends Main
{
    /**
     * 管理员列表
    */
    public function history(){
        $where = [];
        $keyword = $this->request->param('keyword');

        if($keyword){
            $where[] = ['period','like','%'.$keyword.'%'];
        }

        $data = Db::name('double_history')
            ->alias('a')
            ->where($where)
            ->order('period desc')
            ->paginate(15, false, [
                'query' => request()->param()]);
        $this->assign('list', $data);
        return $this->fetch('index');
    }

    //增加
    public function historyAdd()
    {
        $request = $this->request;
        if($request->isPost()){
            $post     = $request->post();
            $post['create_time'] = date('Y-m-d H:i:s', time());
			$data = Db::name('double_history')
                ->where('period', $post['period'])
                ->find();
            if(empty($data)){
            	$db = Db::name('double_history')
	                ->insert($post);
            }
            $this->success('添加成功');
        }else{
            return $this->fetch();
        }

    }

    //编辑提交
    public function historyEdit($id)
    {
        $request = $this->request;
        if($request->isPost()){
            $post     = $this->request->post();
            $db = Db::name('double_history')
                ->where('id', $post['id'])
                ->update($post);
            $this->success('编辑成功');
        }else{
            $data = Db::name('double_history')
                ->where('id', $id)
                ->find();
            $this->assign('data', $data);
            return $this->fetch('edit');
        }
    }
	 //预测
    public function forecast()
    {
    	$where = [];
        $keyword = $this->request->param('keyword');

        if($keyword){
            $where[] = ['period','like','%'.$keyword.'%'];
        }

        $data = Db::name('double_forecast')
            ->alias('a')
            ->where($where)
            ->order('period desc')
            ->paginate(15, false, [
                'query' => request()->param()]);
        $this->assign('list', $data);
        return $this->fetch();
       
    }
    //增加
    public function forecastAdd()
    {
        $request = $this->request;
        if($request->isPost()){
            $post     = $request->post();
			$data = Db::name('double_forecast')
                ->where('period', $post['period'])
                ->find();
            $post['create_time'] = date('Y-m-d H:i:s', time());
            if(empty($data)){
            	$db = Db::name('double_forecast')
	                ->insert($post);
            }

            $this->success('添加成功');
        }else{
            return $this->fetch();
        }

    }
	//编辑提交
    public function forecastEdit($id)
    {
        $request = $this->request;
        if($request->isPost()){
            $post     = $this->request->post();
            $db = Db::name('double_forecast')
                ->where('id', $post['id'])
                ->update($post);
            $this->success('编辑成功');
        }else{
            $data = Db::name('double_forecast')
                ->where('id', $id)
                ->find();
            $this->assign('data', $data);
            return $this->fetch();
        }
    }
   
    //幻圆图
    public function analysis()
    {
        return $this->fetch();
       
    }
    
	//爬虫
    public function crawler()
    {
        return $this->fetch();
       
    }
    //爬虫
    public function crawlerPage()
    {
        return $this->fetch();
       
    }
    
    public function inData(){
    	$period = input('period');
    	$rednum = input('rednum');
    	$bluenum = input('bluenum');
    	$rednum = explode(" ", $rednum);
		if($period){
			$insert = [
				'period' => $period,
				'red1' => $rednum[0],
				'red2' => $rednum[1],
				'red3' => $rednum[2],
				'red4' => $rednum[3],
				'red5' => $rednum[4],
				'red6' => $rednum[5],
				'blue' => $bluenum,
				'create_time' => date("Y-m-d H:i:s"),
			];
			$order = Db::name('double_history')
				->where('period',$period)
				->find();
			if(empty($order)){
				$db = Db::name('double_history')
	                ->insertGetId($insert);
			}
		}
    	$this->success('提交成功','',$period);
    }

    //爬虫
    public function crawlerQuery(){
    	$urls = input('urls','https://chart.cp.360.cn/zst/ssq?r_a=UZFZnq');
    	
//  	$url='http://www.domain.com/?para=123';
		$html= file_get_contents($urls);
		$content = mb_convert_encoding( $html, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5' );
    	
    	
    	preg_match_all('/<table ([\s\S]+?)<\/table>/',$content,$matches);
    	
    	return $matches;
    	$this->success('提交成功','',$response);
    }
    
    public function fquery(){
    	$response = Db::name('double_forecast')
    		->order('period desc')
    		->select();
    	$data = array();
		$childArr = array();
		foreach($response as $key => $value){
			$childArr = array('red1'=>$value['red1'],'red2'=>$value['red2'],'red3'=>$value['red3'],'red4'=>$value['red4'],'red5'=>$value['red5'],'red6'=>$value['red6']);
			$data[$key] = array('id'=>$value['id'],'period'=>$value['period'],'fred'=>$childArr,'fblue'=>$value['blue']);
		}
    	$this->success('提交成功','',$data);
    	
    }
    
    
    public function query(){
    	$response = Db::name('double_history')
    		->order('period desc')
    		->select();
    	$data = array();
		$childArr = array();
		foreach($response as $key => $value){
			$childArr = array('red1'=>$value['red1'],'red2'=>$value['red2'],'red3'=>$value['red3'],'red4'=>$value['red4'],'red5'=>$value['red5'],'red6'=>$value['red6']);
			$data[$key] = array('id'=>$value['id'],'period'=>$value['period'],'red'=>$childArr,'blue'=>$value['blue']);
		}
    	$this->success('提交成功','',$data);
    	
    }
    
    
    
    public function GetYktEmpInfo($url, $data = '', $SOAPAction = '') {
		$respose = $this->reqpost($url, $data, $SOAPAction);
//		$respose = str_replace("soap:", "", $respose);
//		$xml = simplexml_load_string($respose);
//		$data = $xml -> Body;
		return $respose;
	}
	
	public function reqpost($url, $post_data = '', $SOAPAction = '') {
		$ch = curl_init();
		curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
		$headers = array("Content-type:text/xml; charset=utf-8", "SOAPAction:" . $SOAPAction);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POST, true);
//		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
		curl_setopt($ch, CURLOPT_HEADER, false);
		$file_contents = curl_exec($ch);
		$a = curl_getinfo($ch);
		return $file_contents;
	}
    //删除用户
    /*public function deleteAdmin()
    {
        $id = $this->request->post('id');
        $username =  Db::name('admin')
            ->where('id',$id)
            ->value('username');
        if ((int) $id !== 1) {
            if($username!==session('username')){
                 $db = Db::name('admin')
                ->where('id', $id)
                ->setField('status');
                $this->success('删除成功');
            }else{
                 $this->error('无法删除当前登录用户');
            }
        } else {
            $this->error('超级管理员无法删除');
        }
    }*/
}
